package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.message.SystemMessageConfigDTO;
import com.qumitech.visualnovel.controller.vo.message.SystemMessageVO;
import com.qumitech.visualnovel.service.account.AccountGroupService;
import com.qumitech.visualnovel.service.account.AccountService;
import com.qumitech.visualnovel.service.message.SystemMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: yilu
 * @Date: 2019/6/21 13:43
 * @Description:
 */
@RestController
@RequestMapping("/api/message")
public class SystemMessageController {
    @Autowired
    private SystemMessageService systemMessageService;

    @Autowired
    private AccountGroupService groupService;

    @PostMapping("/system")
    public BaseRsp addSystemMsg(@RequestBody SystemMessageVO systemMessageVO){
        systemMessageService.sendScheduleMsg(systemMessageVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/system")
    public BaseRsp deleteSystemMsg(@RequestParam(name = "msg_id") String msgId){
        systemMessageService.deleteScheduleMsg(msgId);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/system")
    public BaseRsp getSystemMsg(@RequestParam(name = "limit", required = false) Integer limit,
                                @RequestParam(name = "offset", required = false) Integer offset,
                                @RequestParam(name = "start_time", required = false) Long startTime,
                                @RequestParam(name = "end_time", required = false) Long endTime){
        return new BaseRsp(ResultEnum.SUCCESS, systemMessageService.getScheduleMsg(offset, limit, startTime, endTime));
    }

    @PutMapping("/system")
    public BaseRsp updateSystemMessage(@RequestBody SystemMessageVO systemMessageVO){
        systemMessageService.updateScheduleMsg(systemMessageVO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }


    @PostMapping("/config")
    public BaseRsp addSystemMessageConfig(@RequestBody SystemMessageConfigDTO systemMessageConfigDTO) {
        byte sendType = 4;
        if (systemMessageConfigDTO.getSendType() == sendType) {
            systemMessageConfigDTO.setSendType((byte)0);
            List<String> userGroupIds = groupService.getGroups(systemMessageConfigDTO.getUserGroup(), 200);
            // 分批发送处理
            for (String uids: userGroupIds) {
                systemMessageConfigDTO.setUids(uids);
                systemMessageService.addSystemMessageConfig(systemMessageConfigDTO);
            }
        } else {
            systemMessageService.addSystemMessageConfig(systemMessageConfigDTO);
        }

        return new BaseRsp(ResultEnum.SUCCESS);
    }


    @PutMapping("/config/{config_id}")
    public BaseRsp updateSystemMessageConfig(@PathVariable("config_id") String configId, @RequestBody SystemMessageConfigDTO systemMessageConfigDTO) {
        systemMessageConfigDTO.setConfigId(configId);
        systemMessageService.updateSystemMessageConfig(systemMessageConfigDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/config/{config_id}")
    public BaseRsp removeSystemMessageConfig(@PathVariable("config_id") String configId) {
        systemMessageService.removeSystemMessageConfig(configId);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/config")
    public BaseRsp getSystemMessageConfigDTO(@RequestParam(name = "limit", required = false) Integer limit,
                                             @RequestParam(name = "offset", required = false) Integer offset,
                                             @RequestParam(name = "state", required = false) Byte state,
                                             @RequestParam(name = "config_id", required = false) String configId,
                                             @RequestParam(name = "msg_category_type", required = false) Byte msgCategoryType,
                                             @RequestParam(name = "startTime", required = false) String startTime,
                                             @RequestParam(name = "endTime", required = false) String endTime,
                                             @RequestParam(name = "operator", required = false) String operator) {
        return new BaseRsp(ResultEnum.SUCCESS,systemMessageService.listSystemMessageConfigDTO(  msgCategoryType,operator,configId, state, startTime, endTime,limit,offset));
    }
}
